package Chuong07;

import java.io.*;

public class Bai08 {

	private static FileReader fR;
	private static FileWriter fW;
	private static BufferedReader bR;
	private static BufferedWriter bW;
	String filename = "D:/Hoc Tap/Hoc Ki VII/Chuyen De Java/OnTap_Code/OnTap/src/Chuong07/File/danhsach1.dat";
	//String filename = "Z:/danhsach.dat";
	
	public Bai08(){
		Create();
		Read();
	}
	
	public void Create(){
		try {
			fW = new FileWriter(filename);
			bW = new BufferedWriter(fW);
			int n = (int)(100*Math.random());			
			//System.out.print("Hệ thống sinh ra: " + n + " phần tử\nDanh sách các phần tử tự sinh là:");
			bW.write(n + "");bW.newLine();
			for(int i = 0; i < n; i++){
				float pt = (float)(100.0*Math.random());
				bW.write(pt + " ");
				//System.out.print(pt + " ");
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		try { 
			bW.flush();
			bW.close(); 
			fW.close();	
		} 
		catch (IOException e) { 
			e.printStackTrace();
		}
	}
	
	public void Read(){
		try {
			fR = new FileReader(filename);
			bR = new BufferedReader(fR);
			String i = bR.readLine();
			String d = "";
			while (i != null) {
				System.out.println(i);
				i = bR.readLine();
				if(i!=null)d=i;
				
			};//System.out.println(d);
			
			String [] ar = d.split(" ");
			float tong = 0;
			for(int j = 0; j < ar.length; j++){
				tong+= Float.parseFloat(ar[j]);
			}System.out.println("Tổng của dãy số là: " + tong);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try { 
			fR.close(); 
			bR.close();
		} 
		catch (IOException e) { 
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Bai08();
	}

}
